package LeetCode;

import java.util.*;

public class Code49_groupAnagrams {

    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs == null || strs.length == 0){
            return new ArrayList<>();
        }
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] c = strs[i].toCharArray();
            Arrays.sort(c);
            String s = String.valueOf(c);
            if (!map.containsKey(s)){
                map.put(s, new ArrayList<>());
            }
            map.get(s).add(strs[i]);
        }

        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        Code49_groupAnagrams code49_groupAnagrams = new Code49_groupAnagrams();
        List<List<String>> res = code49_groupAnagrams.groupAnagrams(
                new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}
        );
        System.out.println(res.size());
    }
}
